<style>
	page{
		background-color: white;
	}
</style>
<style lang="less" scoped>
	.title{
		font-size: 48rpx;
		color: #1a1a1a;
		padding: 50rpx;
	}
		.btn {
			margin-top: 60rpx;
			width: 650rpx;
			height: 88rpx;
			opacity: 1;
			background: #409eff;
			border-radius: 44rpx;
			color:#fff;
			font-size: 32rpx;
		}

		.btn:after {
			border: none;
		}
	.ipt{
		width: 650rpx;
		height: 98rpx;
		font-size: 48rpx;
		border-bottom:  2px solid rgba(0,0,0,0.10);
		margin: 50rpx;
	}
</style>

<template>
	<view class="content">
		<view class="title">绑定手机号</view>
		<input class="ipt" v-model="phone" type="number" placeholder-style="color:#c0c4cc;" placeholder="请输入手机号" />
		<button class="btn cfff f32 flex-center"  type="default"
			@click="getUserProfile">绑定</button>
		
		
		
		
	
	</view>
</template>

<script>
	import pop from '@/static/js/popular.js'
	export default {
		data() {
			return {
				phone:'',
				canIUseGetUserProfile: false,
			};
		},
		onLoad() {
			if (wx.getUserProfile) {

				this.canIUseGetUserProfile = true
			

			}
		},
		methods: {
			
			getUserInfo(e) {
				
				var that = this;

				uni.login({
					provider: 'weixin',
					success: (loginRes) => {
						console.log('loginRes1', loginRes)
						var code = loginRes.code;
						uni.getUserInfo({
							success: (userinfo) => {
								console.log(userinfo)
								var userInfo = userinfo.userInfo
								this.$pop.ajaxPost('/firstLoginWeChat', {
									code,
									phone:this.phone,
									headPortrait:userInfo.avatarUrl
									
								}).then(res => {
									if(res.code==-1){
										uni.showModal({
											content:res.msg
										})
									}else{
										uni.setStorageSync('token',res.body.token)
										uni.setStorageSync('depts',res.body.depts)
										uni.setStorageSync('imgUrl', res.body.INTERNET_FILE_BASE_URL)
										if(res.body.depts.length>0){
											uni.setStorageSync('dept', res.body.depts[0])
										}
										uni.setStorageSync('username', res.body.user)	
										this.$msg('登录成功')
										uni.$emit('login')
										getApp().getdic()
										getApp().getunit()
										getApp().getuser()
										setTimeout(()=>{
											uni.switchTab({
												url:'/pages/index/index'
											})
										},1000)
									}
								})


							}
						})
					}

				})


			},
			getUserProfile(e) {
				
				var userinfo = uni.getStorageSync('userinfo')
					
				uni.login({
					provider: 'weixin',
					success: (loginRes) => {
						console.log('loginRes', loginRes)
						var code = loginRes.code;
						this.$pop.ajaxPost('/firstLoginWeChat', {
							code,
							phone:Number(this.phone),
							headPortrait:userinfo.avatarUrl
							
						}).then(res => {
							if(res.code==0){
								let isHxey = false
								if (res.edition?.tag) {
									isHxey = res.edition.tag === 'hxey'
								
								}
								uni.setStorageSync('hxey', isHxey)
								uni.setStorageSync('token',res.body.token)
								uni.setStorageSync('depts',res.body.depts)
								uni.setStorageSync('edition',res.edition)
								uni.setStorageSync('phone', res.body.phone)	
								uni.setStorageSync('permissions', res.body.permissions)
								this.$msg('登录成功')
								setTimeout(()=>{
									uni.switchTab({
										url:'/pages/index/index'
									})
								},1000)
							}else{
								uni.showModal({
									content:res.msg
								})
							}
						})
					}
				})


			},

		}
	};
</script>
